package com.gurb.android;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import java.io.IOException;

public class FinishedPlayingListener implements OnCompletionListener {

	private MusicList musicList = null;
	private String currentlyPlaying = null;
	
	public FinishedPlayingListener(MusicList musicList, String currentlyPlaying){
		this.musicList = musicList;
		this.currentlyPlaying = currentlyPlaying;
	}
	
	@Override
	public void onCompletion(MediaPlayer mPlayer) {
		String next = musicList.getNext(currentlyPlaying);
		musicList.remove(currentlyPlaying);
		
//		TODO Delete the cast but not now, not this stage of the game, no.
//		File file = casts.remove(0);
//		file.delete();
		
		if (next == null) return;
		try {
			mPlayer.setDataSource(musicList.getFilePath(next));
			mPlayer.prepare();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		mPlayer.start();
		currentlyPlaying = next;
		mPlayer.setOnCompletionListener(this);
	}

}
